package com.fat.spzx.manager.service.impl;

import com.fat.spzx.manager.mapper.SysRoleMenuMapper;
import com.fat.spzx.manager.service.SysMenuService;
import com.fat.spzx.manager.service.SysRoleMenuService;
import com.fat.spzx.model.dto.system.AssginMenuDto;
import com.fat.spzx.model.entity.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查询全部角色菜单和根据角色id查询角色菜单
     */
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {
        HashMap<String, Object> resultMap = new HashMap<>();
        //1.查询全部角色菜单
        List<SysMenu> sysMenuList = sysMenuService.findNodes();
        //2.根据角色id查询角色菜单
        List<Long> roleMenuList = sysRoleMenuMapper.findSysRoleMenuByRoleId(roleId);
        //3.封装数据
        resultMap.put("sysMenuList", sysMenuList);
        resultMap.put("roleMenuIds", roleMenuList);
        return resultMap;
    }

    /**
     * 保存角色菜单
     */
    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
       //1.删除角色菜单
       sysRoleMenuMapper.deleteByRoleId(assginMenuDto.getRoleId());
       //2.添加角色菜单
        List<Map<String, Number>> menuIdList = assginMenuDto.getMenuIdList();
        if (menuIdList != null && !menuIdList.isEmpty()){
            sysRoleMenuMapper.saveRoleMenu(assginMenuDto);
        }
    }
}
